package com.example.devicemanage.controller;

import com.example.devicemanage.entity.Repair;
import com.example.devicemanage.service.inter.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class RepairController {
    @Autowired
    RepairService repairService;
    @Autowired
    EquipController equipController;

    @RequestMapping(path = "/insertRepair", method = RequestMethod.POST)
        //获取请求参数方式3,直接使用原生的HttpServletRequest
    void insertRepair(HttpServletRequest httpServletRequest) {
        String eid = httpServletRequest.getParameter("eid");
        System.out.println(eid);
        equipController.updateEquipstate(eid);
        String rcosts = httpServletRequest.getParameter("rcosts");

        String rmanu = httpServletRequest.getParameter("rmanu");
        String rdate = httpServletRequest.getParameter("rdate");
        String master = httpServletRequest.getParameter("master");
        Repair repair = new Repair();
        repair.setEid(eid);
        repair.setRcosts(Integer.parseInt(rcosts));
        repair.setRdate(rdate);
        repair.setMaster(master);
        repair.setRmanu(rmanu);
        Boolean aBoolean = repairService.saveRepair(repair);

//     return equipController.queryAllequip(httpServletRequest);
//        return "equip_list";

    }
    @ResponseBody
    @RequestMapping(path = "/repair_update", method = RequestMethod.POST)
        //获取请求参数方式3,直接使用原生的HttpServletRequest
    Boolean updateRepair(HttpServletRequest httpServletRequest) {
        String eid = httpServletRequest.getParameter("eid");
        System.out.println(eid);
        String repairid= httpServletRequest.getParameter("repairid");
        String rcosts = httpServletRequest.getParameter("rcosts");

        String rmanu = httpServletRequest.getParameter("rmanu");
        String rdate = httpServletRequest.getParameter("rdate");
        String master = httpServletRequest.getParameter("master");
        Repair repair = new Repair();
        repair.setEid(eid);
        repair.setRepairid(Integer.parseInt(repairid));
        repair.setRcosts(Integer.parseInt(rcosts));
        repair.setRdate(rdate);
        repair.setMaster(master);
        repair.setRmanu(rmanu);
        Boolean aBoolean = repairService.updateRepair(repair);

//     return equipController.queryAllequip(httpServletRequest);
//        return "equip_list";
return aBoolean;
    }

    @RequestMapping(path = "/repair_list", method = RequestMethod.GET)
    public String getRepair_list(HttpServletRequest httpServletRequest) {
        List<Repair> list = repairService.showAllRepair();
        httpServletRequest.setAttribute("list", list);

        httpServletRequest.setAttribute("repairNum", list.size());
        return "repair_list";
    }

    @ResponseBody
    @RequestMapping(path = "/repair_delete")
    public String getRepair_delete(int repairid) {


        return repairService.deleteByid(repairid) == 1 ? "finished" : "wrong";

    }
}
